package cn.tedu;

import cn.tedu.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Date：2022年03月
 * Author：程Sir
 * Name： JDBC_project
 * Content:
 *  获取自增主键
 */
public class JdbcAutoKeys {
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入要查询第几页的数据？");
//        int page = scanner.nextInt();
//        System.out.println("请输入每页显示多少条数据？");
//        int pageSize = scanner.nextInt();

        // 获取连接
        try(Connection connection = JDBCUtils.getConnection()){
            // sql
            String sql = "insert into user values(null, ?, ?)";
            // 获取preparedStatement对象
            PreparedStatement preparedStatement = connection.prepareStatement(sql, 1);
            // 给占位符赋值
            preparedStatement.setString(1,"cheng");
            preparedStatement.setString(2,"123456");
            //执行计划
            preparedStatement.executeUpdate();
            // 获取自增主键的结果集
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            // 取数据
            while (generatedKeys.next()){
                System.out.println(generatedKeys.getInt(1));
            }
            System.out.println("执行完成！");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}
